﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;

namespace Dotnet.Utils.Utility.ObjectExtensions.Date
{
    /// <summary>
    /// 时间戳相关扩展
    /// </summary>
    public static class DateTimeStampExtension
    {
        /// <summary>
        /// 13位Unix时间戳转为DateTime
        /// </summary>
        /// <param name="timestamp">1970年到目标时间的毫秒数</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this long timestamp) => new DateTime(1970, 1, 1).ToLocalTime().AddMilliseconds(timestamp);

        /// <summary>
        /// 10位Linux时间戳转为DateTime
        /// </summary>
        /// <param name="timestamp">1970年到目标时间的秒数</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this int timestamp) => new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(timestamp);

        /// <summary>
        /// DateTime时间格式转换为13位Unix时间戳 1970年1月1日到现在的毫秒数
        /// </summary>
        /// <param name="time">目标时间</param>
        /// <returns></returns>
        public static long ToUnixTimeStamp_13bit(this DateTime time) => (long)(time - new DateTime(1970, 1, 1).ToLocalTime()).TotalMilliseconds;

        /// <summary>
        /// DateTime时间格式转换为10位Linux时间戳 1970年1月1日到现在的秒数
        /// </summary>
        /// <param name="time">目标时间</param>
        /// <returns></returns>
        public static int ToLinuxTimeStamp_10bit(this DateTime time) => (int)(time - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
    }

    public class DateTimeConverter : IsoDateTimeConverter
    {
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (objectType == typeof(DateTime))
            {
                string text = reader.Value.ToString();
                if (string.IsNullOrEmpty(text))
                {
                    return existingValue;
                }
                if (text.Length == 19 && text[10] == 'T' && text[4] == '-' && text[13] == ':')
                {
                    return Convert.ToDateTime(text);
                }
            }
            return base.ReadJson(reader, objectType, existingValue, serializer);
        }
    }
}